import {Component, OnInit} from '@angular/core';
import {PageEntity} from '../../../core/domain/page.entity';
import {StatusEnum} from '../../../core/enum/status.enum';
import {NzMessageService} from 'ng-zorro-antd';
import {FormBuilder} from '@angular/forms';
import {DatePipe} from '@angular/common';
import {LessonManageService} from './lesson-manage.service';

@Component({
    selector: 'app-lesson-manage',
    templateUrl: './lesson-manage.component.html',
    providers: [LessonManageService]
})
export class LessonManageComponent implements OnInit {
    // 分页参数对象
    pageInfo: PageEntity = new PageEntity();
    // 点击显示开关
    setTimeShow = false;
    // 表格数据数组
    lessonData: any[] = [];
    // 时间参数类
    setTimeParam: TimeParam = new TimeParam();
    // 弹窗确认时间参数
    // 课节
    lessonBite: string;
    // id 参数
    ids: string;

    constructor(private service: LessonManageService,
                private fb: FormBuilder,
                private message: NzMessageService,
                private  datePipe: DatePipe) {
    }

    ngOnInit() {
        this.pageInfo.rows = 14;
        this.getSectionPage(this.pageInfo);
    }

    /**
     * 课节管理分页数据接口
     * @ param page
     */
    getSectionPage(page): void {
        this.service.getSectionPage(page).subscribe(res => {
            if (res.status === StatusEnum.SUCCESS) {
                this.lessonData = res.datas;
            }
        });
    }

    /**
     * 点击设置时间逻辑函数
     * @ param data
     */
    setTimeFn(data: any): void {
        // 时间格式转换
        this.ids = data.id;
        const date = data.startTime.split(':');
        const endDate = data.endTime.split(':');
        const cd = new Date();
        // 转换成中国标准时间
        const startTime = new Date(cd.getFullYear(), cd.getMonth(), cd.getDay(),
            date.length >= 1 ? date[0] : '', date.length >= 2 ? date[1] : '', date.length >= 3 ? date[2] : '');
        const endTime = new Date(cd.getFullYear(), cd.getMonth(), cd.getDay(),
            endDate.length >= 1 ? endDate[0] : '', endDate.length >= 2 ? endDate[1] : '', endDate.length >= 3 ? endDate[2] : '');
        this.service.timeSetVerify(data.sectionNumber.toString()).subscribe(res => {
            if (res.status === StatusEnum.SUCCESS) {
                if (res.datas) {
                    this.setTimeShow = true;
                    this.setTimeParam = {
                        startTime,
                        endTime
                    };
                    this.lessonBite = data.sectionName;
                }
            } else {
                this.message.warning('存在未完成的预约,不能设置时间');
            }
        });

    }

    /**
     * 时间弹窗点击确认
     */
    timeSetComfirFn() {
        // 时-分-秒切割转换
        const startT = this.datePipe.transform(this.setTimeParam.startTime, 'HH:mm:ss');
        const endT = this.datePipe.transform(this.setTimeParam.endTime, 'HH:mm:ss');
        let obj: { startTime: string; endTime: string; id: string };
        obj = {
            startTime: startT,
            endTime: endT,
            id: this.ids
        };
        this.service.setTime(obj).subscribe(res => {
            if (res.status === StatusEnum.SUCCESS) {
                this.setTimeShow = false;
                // this.pageInfo.first = 1;
                this.pageInfo.rows = 14;
                this.getSectionPage(this.pageInfo);
                this.message.success('设置成功');
            }
        });
    }
}

/**
 * 时间参数
 */
class TimeParam {
    startTime: any;
    endTime: any;
}

